package yxy.game.pm2.bean.timeline;

import org.joda.time.DateTime;

import yxy.game.pm2.bean.module.BaseGameRedisBean;

/** 时间轴模块 */
public abstract class TimelineModule extends BaseGameRedisBean {
	// bean===========================================================================

	private Timeline timeline;

	public TimelineModule() {
	}

	public void setTimeline(Timeline timeline) {
		this.timeline = timeline;
	}

	public Timeline getTimeline() {
		return timeline;
	}

	// func===========================================================================

	public Timeline timeline() {
		if (timeline == null) {
			setTimeline(new Timeline());
		}
		return timeline;
	}

	/** 是否正在运行 */
	public boolean running() {
		// 起始时间未设定
		DateTime start = timeline().getStartTime();
		DateTime end = timeline().getEndTime();
		if (start == null) {
			return true;
		}
		DateTime now = DateTime.now();
		// 结束时间未设定
		if (end == null) {
			if (now.isBefore(start)) {
				// 当前时间在起始之前
				return false;
			}
			return true;
		}
		// 时间设定检查
		if (start.isAfter(end)) {
			return false;
		}
		int round = timeline().getRound();//轮次
		long circleTime = timeline().getCircleTime();//循环间隔时间
		// 周期时间校正 循环间隔时间不能小于结束时间减去开始时间
		if (circleTime < (end.getMillis() - start.getMillis())) {
			circleTime = end.getMillis() - start.getMillis();
		}
		int times = 1;
		// 轮次检查 当前在结束时间之前  轮次信息大于0   已循环轮次小于设定轮次
		while (end.isBefore(now) && round > 0 && times < round) {
			start = start.plus(circleTime);
			end = end.plus(circleTime);
			times++;
		}
		if (round > 0 && times > round) {
			return false;
		}

		// 起始和结束都设定好 根据周期和轮次判断
		if (now.isBefore(start) || end.isBefore(now)) {
			return false;
		}
		return true;
	}

}
